{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_csv(file, lst, cats):\n",
    "    with open(file, newline='') as f:\n",
    "        reader = csv.reader(f)\n",
    "        for i,row in enumerate(reader):\n",
    "            cat = row[1]\n",
    "            lst.append(row[0])\n",
    "            cats.append(int(row[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "cats = []\n",
    "read_csv(\"data.csv\", data, cats)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataWriter = csv.writer(open('data_merged.csv', 'w'), delimiter=',',lineterminator=\"\\n\")\n",
    "for n,point in enumerate(data):\n",
    "    category = cats[n]\n",
    "    is_happy = category == 3 or category == 5 or category == 6 or category == 7 or category == 9 or category == 11 \n",
    "    is_neutral = category == 0 or category == 1 or  category == 10\n",
    "    is_sad = category == 2 or category == 4\n",
    "    if is_happy:\n",
    "        dataWriter.writerow([point, 1])\n",
    "    elif is_neutral:\n",
    "        dataWriter.writerow([point, 0])\n",
    "    elif is_sad:\n",
    "        dataWriter.writerow([point, 2])\n",
    "    elif category == 8:\n",
    "        dataWriter.writerow([point, 3])\n",
    "    elif category == 12:\n",
    "        dataWriter.writerow([point, 4])\n",
    "    else:\n",
    "        print(point,n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "with open('data_merged.csv') as csvfile:\n",
    "    readCSV = csv.reader(csvfile, delimiter=',')\n",
    "    for row in readCSV:\n",
    "        count+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45309"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
